package com.dwjm.exam.stu_list;

import com.dwjm.exam.pojo.Student;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/students")
public class StudentListHandler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        try {
            StudentDao studentDao = new StudentDaoImpl();
            String name = req.getParameter("name");
            int page = Integer.parseInt(req.getParameter("page"));
            int size = Integer.parseInt(req.getParameter("size"));

            System.out.println("分页请求参数: page=" + page + ", size=" + size);
// 获取会话中的用户类型和学生ID
            HttpSession session = req.getSession();
            String userType = (String) session.getAttribute("userType");
            String loggedInStudentId = (String) session.getAttribute("studentId");

// 根据用户类型获取学生列表
            List<Student> studentList;
            int totalCount;

            if ("student".equals(userType) && loggedInStudentId != null) {
                // 如果是学生用户，只获取当前登录学生的信息
                studentList = studentDao.getStudentById(loggedInStudentId);
                totalCount = studentList.size(); // 只有当前学生一条记录
            } else {
                // 如果是管理员或其他用户，获取所有学生信息
                totalCount = studentDao.count(name);
                studentList = studentDao.List(name, page, size);
            }

            // 获取商品总数以计算总页数
//            int totalCount = studentDao.count(name);
//            System.out.println("商品总数: " + totalCount);

            // 计算总页数
            int totalPages = (int) Math.ceil((double) totalCount / size);
            if (totalPages < 1) totalPages = 1;
            System.out.println("计算总页数: " + totalPages);

            // 获取当前页数据
            List<Student> allGoods = studentDao.List(name,page, size);

            // 构建响应
            Map<String, Object> resultMap = new HashMap<>();
            if (allGoods != null && allGoods.size() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "success");
                resultMap.put("data", allGoods);
                resultMap.put("totalPages", totalPages);
                resultMap.put("currentPage", page);
            } else {
                resultMap.put("code", 500);
                resultMap.put("msg", "没有对应的数据");
                resultMap.put("data", null);
                resultMap.put("totalPages", 0);
                resultMap.put("currentPage", page);
            }

            // 使用Gson转换成JSON
            Gson gson = new Gson();
            String s = gson.toJson(resultMap);
            resp.getWriter().write(s);
        } catch (Exception e) {
            System.out.println("获取商品列表时发生异常: " + e.getMessage());
            e.printStackTrace();

            // 发送错误响应
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("code", 500);
            errorMap.put("msg", "系统异常: " + e.getMessage());

            Gson gson = new Gson();
            resp.getWriter().write(gson.toJson(errorMap));
        }
    }
}
